<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Controller\AdminController;

class SeckillController extends AdminController{
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'operate'); // 数据集
    } 
    //秒杀列表展示
    public function index(){
        $tp = I('tp');
        if($tp == 'seckill'){
            $name = '秒杀';
            $where['seckil_type'] = 1;
        }else{
            $name = '限时折扣';
            $where['seckil_type'] = 2;
        }
        $this->assign('tp',$tp);
        $this->assign('name',$name);// 赋值数据集
        
        
        $s = M('seckill');
        $count      = $s->where($where)->order('seckill_add_time desc')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $s->where($where)->order('seckill_add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('seckill',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('action', $tp);
        $this->display();
    }

    //添加秒杀活动
    public function add(){
        $tp = I('tp');
        if($tp == 'seckill'){
            $name = '秒杀';
            $seckil_type = 1;
        }else{
            $name = '限时折扣';
            $seckil_type = 2;
        }
        $this->assign('name',$name);// 赋值数据集
        
        if(IS_POST){
            $data['seckill_title'] = I('seckill_title');
            $data['seckil_type'] = $seckil_type;
            $data['seckill_describe'] = I('seckill_describe');
            $data['seckill_price'] = I('seckill_price');
            $data['seckill_start_time'] = strtotime(I('seckill_start_time'));
            $data['seckill_end_time'] = strtotime(I('seckill_end_time'));
            $data['seckill_nums'] = I('seckill_nums');
            $data['seckill_limit'] = I('seckill_limit');
            $data['seckill_state'] = I('seckill_state');
            $data['seckill_goods_id'] = I('goods_id');
            $data['store_id'] = 0;
            $data['store_name'] = "淘嘟嘟平台";
            $data['seckill_add_time'] = NOW_TIME;
            $re = M('seckill')->data($data)->add();
            if($re){
                $this->success('新增成功',U('Seckill/index',array('tp'=>$tp)));
            }else{
                $this->error('失败，请稍后重试');
            }
        }
        $where['voucher_state'] = 1;
        $p_voucher_rule = M('p_voucher_rule')->where($where)->select();
        $this->assign('p_voucher_rule',$p_voucher_rule);
        $this->assign('action', $tp);
        $this->display();
    }

    public function edit(){
        $tp = I('tp');
        if($tp == 'seckill'){
            $name = '秒杀';
        }else{
            $name = '限时折扣';
        }
        $this->assign('name',$name);// 赋值数据集
        
        if(IS_POST){

            $where['seckill_id'] = I('seckill_id');
            $data['seckil_type'] = I('seckil_type');
            $data['seckill_title'] = I('seckill_title');
            $data['seckill_describe'] = I('seckill_describe');
            $data['seckill_price'] = I('seckill_price');
            $data['seckill_start_time'] = strtotime(I('seckill_start_time'));
            $data['seckill_end_time'] = strtotime(I('seckill_end_time'));
            $data['seckill_nums'] = I('seckill_nums');
            $data['seckill_limit'] = I('seckill_limit');
            $data['seckill_state'] = I('seckill_state');
            $data['seckill_goods_id'] = I('goods_id');
            $data['store_id'] = I('store_id');
            $data['store_name'] = I('store_name');
            $re = M('seckill')->where($where)->data($data)->save();
            if($re !== false){
                $this->success('修改成功',U('Seckill/index',array('tp'=>$tp)));
            }else{
                $this->error('失败，请稍后重试');
            }

        }
        $seckill = M('seckill')->where(array('seckill_id'=>I('seckill_id')))->find();
        $goods = M('goods')->field('goods_id,goods_thumb,goods_title')->where('goods_id='.$seckill['seckill_goods_id'])->find();
        $this->assign('goods',$goods);
        $this->assign('seckill',$seckill);
        $this->assign('action', $tp);
        $this->display();
    }

    public function del(){
        $re = M('seckill')->where(array('seckill_id'=>I('post.data')))->delete();
        if($re){
            $this->success('删除成功', 'index');
        }else{
            $this->error('失败，请稍后重试');
        }
    }
    
   //放入下架ajax
    public function del_tuijian() {
        $where['seckill_id'] = I('seckill_id');
        $rea =M('seckill')->where($where)->getField('seckill_qx');
        
        if($rea == 0){
            $data['seckill_qx'] = 1;
        }else{
            $data['seckill_qx'] = 0;
        }
        $re = M('seckill')->where($where)->data($data)->save();
        if ($re) {
            $data['n'] = true;
        } else {
            $data['n'] = false;
        }
        $this->ajaxReturn($data);
    }


}